﻿#Include ..\baseHandle.ahk

#Include ..\..\_lib\Fs.ahk

class Tmp extends BaseHandle {
  static nullable := true

  static Handle(parsed) {
    if (!parsed.target) {
      openFile('0')
      return this.Succ('default.', 'x')
    }
    if parsed.hasParam('a') {
      f := FileOpen(Path.Join('./cfg/tmp/', parsed.target '.txt'), 'rw', 'utf-8')
      f.Close()
      openFile(parsed.target)
      return this.Succ('ok.', 'x')
    } else {
      cnt := 0
      ([parsed.target, parsed.extra*]).foreach(v => (FileExist('./cfg/tmp/' v '.txt') && openFile(v) && cnt++))
      return this.Succ(cnt, 'x')
    }

    openFile(v) => Run('notepad ./cfg/tmp/' v '.txt')
  }

  static Spec() {
    return {
      cmd: 'tmp',
      desc: '打开临时文件',
      format: 'tmp [-a] [target]',
      params: [{
        name: 'a',
        desc: '不存在时新建'
      }],
      args: {
        desc: '',
        isOptional: false,
        cacheable: false,
        generator: {
          script: '',
          postProcess: Conf
        }
      },
      extra: {
        isEnable: false
      }
    }

    Conf(*) {
      fileNames := []
      FS.ReadDir('./cfg/tmp', Noop, (_, v) => fileNames.Push(v.subString(1, v.Length - 3)), '*.txt')
      return fileNames
    }
  }
}
